package com.hangxingliu.exceltools;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListSelectionModel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import javax.swing.JScrollPane;
import java.awt.Toolkit;
import java.awt.Dimension;
import javax.swing.JTable;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MainWindow extends JFrame {

	private static final long serialVersionUID = 3428709110216461809L;

	private Controller controller = new Controller(this);
	public Attr attr = new Attr();
	
	public JPanel layoutRoot;
	public JTextField textFilePath;
	public JTextField txtStartLine;
	public JTable tbPreview;

	public JButton btnOpen;
	public JLabel labelChooseSheet;
	public JComboBox<String> chooseSheet;
	public JCheckBox checkFullPreview;
	public JCheckBox checkWindowTop;
	public JScrollPane scrollable;
	public JLabel labelDuplicatedCol;
	public JComboBox<String> chooseDuplicatedCol;
	public JLabel labelStartLine;
	public JButton btnFind;
	public JButton btnSave;
	public JButton btnHelp;
	public JTextArea textOutput;
	public JPanel panel;

	public void setupAddonUI() {
		setTitle(String.format("%s v%s(%s) %s", 
			Application.NAME, Application.VERSION, Application.DATE, Application.AUTHOR));
	}
	
	public class Attr {
		public boolean isPreviewAllData() { 
			return checkFullPreview.isSelected(); }
		
		public Integer getStartRow() {
			try { 
				return Integer.parseInt(txtStartLine.getText());
			} catch(NumberFormatException e) { 
				return null;
			}
		}
		
		public void updateOutput(StringBuilder output) {
			textOutput.setText(output.toString());} 
		
		public int getColumn() {
			return chooseDuplicatedCol.getSelectedIndex(); }
		public void setColumn(int index) {
			chooseDuplicatedCol.setSelectedIndex(index);}
		
		public void updateSheetNames(String[] sheetNames) {
			chooseSheet.setModel(new DefaultComboBoxModel<String>(sheetNames));}
		
		public void updateDuplicatedColumns(String[] columnNames) {
			chooseDuplicatedCol.setModel(new DefaultComboBoxModel<String>(columnNames));}
		
		public void updateFilePath(String filePath) {
			textFilePath.setText(filePath);}
		
		public void updatePreviewTable(String[] title, String[][] data) {
			int w = Math.min(150, getWidth() / 5) * title.length,
				h = data.length * 28;
			panel.setPreferredSize(new Dimension(w, h));
			tbPreview.setBounds(0, 0, w, h);
			scrollable.setViewportView(panel);
			tbPreview.setModel(new DefaultTableModel(data, title));}
	}
	
	/**
	 * Create the frame.
	 */
	public MainWindow() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				controller.onWindowClosing();
			}
		});
		setMinimumSize(new Dimension(720, 640));
		setIconImage(Toolkit.getDefaultToolkit().getImage(MainWindow.class.getResource("/com/hangxingliu/exceltools/icon.png")));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 720, 640);
		layoutRoot = new JPanel();
		layoutRoot.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(layoutRoot);
		
		textFilePath = new JTextField();
		textFilePath.setFocusable(false);
		textFilePath.setEditable(false);
		textFilePath.setColumns(10);
		
		btnOpen = new JButton("打开文件");
		btnOpen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				controller.onClickOpen();
			}
		});
		
		labelChooseSheet = new JLabel("表格预览");
		
		chooseSheet = new JComboBox<String>();
		chooseSheet.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if(e.getStateChange() == ItemEvent.SELECTED) {
					Object item = e.getItem();
					if(item != null)
						controller.onUpdateSheetName((String) item);
				}
			}
		});
		
		checkFullPreview = new JCheckBox("完整预览");
		checkFullPreview.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				controller.onUpdatePreviewCheckBox();
			}
		});
		
		checkWindowTop = new JCheckBox("窗口置顶");
		checkWindowTop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setAlwaysOnTop(checkWindowTop.isSelected());
			}
		});
		checkWindowTop.setActionCommand("");
		
		scrollable = new JScrollPane();
		
		labelDuplicatedCol = new JLabel("检查重复的列:");
		
		chooseDuplicatedCol = new JComboBox<String>();
		
		labelStartLine = new JLabel("起始行:");
		
		txtStartLine = new JTextField();
		txtStartLine.setText("1");
		txtStartLine.setColumns(10);
		
		btnFind = new JButton("查找重复");
		btnFind.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				controller.onClickFind();
			}
		});
		
		btnSave = new JButton("导出重复的数据");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				controller.onClickSave();
			}
		});
		
		btnHelp = new JButton("帮助与关于");
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				controller.onClickHelp();
			}
		});
		
		JScrollPane scrollable2 = new JScrollPane();
		GroupLayout gl_layoutRoot = new GroupLayout(layoutRoot);
		gl_layoutRoot.setHorizontalGroup(
			gl_layoutRoot.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_layoutRoot.createSequentialGroup()
					.addGroup(gl_layoutRoot.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_layoutRoot.createSequentialGroup()
							.addComponent(labelChooseSheet)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(chooseSheet, 0, 450, Short.MAX_VALUE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(checkFullPreview))
						.addComponent(textFilePath, GroupLayout.DEFAULT_SIZE, 595, Short.MAX_VALUE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_layoutRoot.createParallelGroup(Alignment.TRAILING)
						.addComponent(btnOpen)
						.addComponent(checkWindowTop, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE)))
				.addComponent(scrollable, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 688, Short.MAX_VALUE)
				.addGroup(gl_layoutRoot.createSequentialGroup()
					.addComponent(labelDuplicatedCol, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(chooseDuplicatedCol, 0, 151, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(labelStartLine)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(txtStartLine, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnFind)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnSave)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnHelp))
				.addComponent(scrollable2, GroupLayout.DEFAULT_SIZE, 688, Short.MAX_VALUE)
		);
		gl_layoutRoot.setVerticalGroup(
			gl_layoutRoot.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_layoutRoot.createSequentialGroup()
					.addGroup(gl_layoutRoot.createParallelGroup(Alignment.BASELINE)
						.addComponent(textFilePath, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnOpen, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_layoutRoot.createParallelGroup(Alignment.BASELINE)
						.addComponent(chooseSheet, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
						.addComponent(checkWindowTop)
						.addComponent(checkFullPreview)
						.addComponent(labelChooseSheet))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollable, GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_layoutRoot.createParallelGroup(Alignment.BASELINE)
						.addComponent(labelDuplicatedCol)
						.addComponent(chooseDuplicatedCol, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnHelp)
						.addComponent(btnSave)
						.addComponent(btnFind)
						.addComponent(txtStartLine, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
						.addComponent(labelStartLine))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollable2, GroupLayout.PREFERRED_SIZE, 195, GroupLayout.PREFERRED_SIZE))
		);
		
		textOutput = new JTextArea();
		textOutput.setEditable(false);
		scrollable2.setViewportView(textOutput);
		
		panel = new JPanel();
		scrollable.setViewportView(panel);
		panel.setLayout(null);
		
		tbPreview = new JTable();
		tbPreview.setCellSelectionEnabled(true);
		tbPreview.setShowVerticalLines(true);
		tbPreview.setRowSelectionAllowed(false);
		tbPreview.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				controller.onTableColumnClick(tbPreview.getSelectedColumn());
			}
		});
		tbPreview.setBounds(0, 0, 1920, 1080);
		tbPreview.setCellEditor(null);
		tbPreview.setRowHeight(25);
		tbPreview.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		panel.add(tbPreview);
		layoutRoot.setLayout(gl_layoutRoot);
		
		setupAddonUI();
	}
}
